<?php
defined('IN_DESTOON') or exit('Access Denied');
 $menus = array (
        array('添加邮件推送用户', '?moduleid='.$moduleid.'&file='.$file.'&action=add'),
		array('邮件推送用户列表', '?moduleid='.$moduleid.'&file='.$file),
	);

switch($action) {
	case 'delete':
		$itemid or msg('请选择记录');
		$itemids = is_array($itemid) ? implode(',', $itemid) : $itemid;
		$db->query("DELETE FROM {$DT_PRE}email_user WHERE itemid IN ($itemids)");
		dmsg('删除成功', $forward);
	break;
	//修改订阅信息
	
	case 'edit':
		$itemid or msg();
		$db->itemid = $itemid;
		if($submit) {
		    if($post['filteraddress']){$filteraddress=implode(",",$post['filteraddress']);}
			if($post['filtertender']){$filtertender=implode(",",$post['filtertender']);}
			if($post['filtercategory']){$filtercategory=implode(",",$post['filtercategory']);}
		    //如果设置为冻结状态
			if($post['pushstatus']==1){
				$post['endtime']=time();
				$post['pushemail']=0;
				
			}else{
			 if($post['pushstatus']==2){$post['endtime']=time();}else{$post['endtime']=strtotime($post['endtime']);}
			}
			
	       $db->query("update {$DT_PRE}email_user set `filteraddress`='".$filteraddress."',`filtertender`='".$filtertender."',`filtercategory`='".$filtercategory."',`email`='".$post['email']."',`pushemail`='".$post['pushemail']."',`pushstatus`='".$post['pushstatus']."',`endtime`='".$post['endtime']."',`telephone`='".$post['telephone']."',`company`='".$post['company']."',`lxr`='".$post['lxr']."',`beizhu`='".$post['beizhu']."' where itemid=".$itemid);
         dmsg('修改成功', $forward);
		} else {
			$item = $db->get_one("SELECT * FROM {$DT_PRE}email_user where itemid=".$itemid);
			extract($item);
			//读取行业分类
			$hangye = array();
			$result = $db->query("SELECT fid,f_cat_name FROM {$DT_PRE}filtercategory ORDER  BY displayorder");
			while($r = $db->fetch_array($result)) {
				$hangye[] = $r;
			}
			//读取服务区域
			$quyu = array();
			$result = $db->query("SELECT areaid,areaname FROM {$DT_PRE}area ORDER  BY listorder");
			while ( $r = $db->fetch_array($result) ) {
				$quyu[] = $r;
			}
			//招标分类
			$category = array();
			$result = $db->query("SELECT catid,catname FROM {$DT_PRE}category WHERE moduleid=23 order by catid ");
			while($r = $db->fetch_array($result)) {
				$category[] = $r;
			}
			$dq1=explode(",",$filteraddress);
			$cat=explode(",",$filtertender);
			$hangye1=explode(",",$filtercategory);
		
			include tpl('email_user_edit', $module);
        }
	break;
	
	case 'add':
		if($submit) {
		    if($post['filteraddress']){$filteraddress=implode(",",$post['filteraddress']);}
			if($post['filtertender']){$filtertender=implode(",",$post['filtertender']);}
			if($post['filtercategory']){$filtercategory=implode(",",$post['filtercategory']);}
		    //如果设置为冻结状态
			if($post['pushstatus']==1){
				$post['email']='';
				$post['endtime']=time();
				$post['pushemail']=0;
				$filteraddress='';
				$filtertender='';
				$filtercategory='';
			}else{
			 if($post['pushstatus']==2){$post['endtime']=time();}else{$endtime=time()+($DT['tuisong']*60*60*24);;}
			}
			$db->query("insert into {$DT_PRE}email_user(`addtime`,`edittime`,`push`,`filtercategory`,`filteraddress`,`filtertender`,`email`,`pushemail`,`pushstatus`,`endtime`,`telephone`,`company`,`lxr`,`beizhu`)value('".time()."','".time()."','1','".$filtercategory."','".$filteraddress."','".$filtertender."','".$post['email']."','".$post['pushemail']."','".$post['pushstatus']."','".$endtime."','".$post['telephone']."','".$post['company']."','".$post['lxr']."','".$post['beizhu']."')");
         dmsg('添加成功', $forward);
		} else {
			$item = $db->get_one("SELECT * FROM {$DT_PRE}email_user where itemid=".$itemid);
			extract($item);
			//读取行业分类
			$hangye = array();
			$result = $db->query("SELECT fid,f_cat_name FROM {$DT_PRE}filtercategory ORDER  BY displayorder");
			while($r = $db->fetch_array($result)) {
				$hangye[] = $r;
			}
			//读取服务区域
			$quyu = array();
			$result = $db->query("SELECT areaid,areaname FROM {$DT_PRE}area ORDER  BY listorder");
			while ( $r = $db->fetch_array($result) ) {
				$quyu[] = $r;
			}
			//招标分类
			$category = array();
			$result = $db->query("SELECT catid,catname FROM {$DT_PRE}category WHERE moduleid=23 order by catid ");
			while($r = $db->fetch_array($result)) {
				$category[] = $r;
			}
			include tpl('email_user_add', $module);
        }
	break;
	
	
    default:
		$sfields = array('邮箱','手机号', '公司名称', '联系人');
		$dfields = array('email', 'telephone', 'company', 'lxr');
		$sorder  = array('结果排序方式', '关注时间降序', '关注时间升序', '更新时间降序', '更新时间升序');
		$dorder  = array('itemid DESC', 'addtime DESC', 'addtime ASC', 'logintime DESC', 'logintime ASC');
		isset($fields) && isset($dfields[$fields]) or $fields = 0;
		isset($site) or $site = '';
		isset($order) && isset($dorder[$order]) or $order = 0;
		$thumb = isset($thumb) ? intval($thumb) : 0;
		$sex = isset($sex) ? intval($sex) : -1;
		$subscribe = isset($subscribe) ? intval($subscribe) : -1;
		$fields_select = dselect($sfields, 'fields', '', $fields);
		$order_select  = dselect($sorder, 'order', '', $order);
		$condition = '1';
		if($keyword) $condition .= " AND $dfields[$fields] LIKE '%$keyword%'";
		if($thumb) $condition .= " AND headimgurl<>''";
		if($sex > -1) $condition .= " AND sex='$sex'";
		if($subscribe > -1) $condition .= " AND subscribe='$subscribe'";
		if($pushstatus){$condition.=" and pushstatus=".$pushstatus;}
		$order = $dorder[$order];
		if($page > 1 && $sum) {
			$items = $sum;
		} else {
			$r = $db->get_one("SELECT COUNT(*) AS num FROM {$DT_PRE}email_user WHERE $condition");
			$items = $r['num'];
		}
		$pages = pages($items, $page, $pagesize);
		$lists = array();
		$result = $db->query("SELECT * FROM {$DT_PRE}email_user WHERE $condition ORDER BY $order LIMIT $offset,$pagesize");
		while($r = $db->fetch_array($result)) {
			//服务区域
			$qy='';
			if($r['filteraddress']){
			 $result1 = $db->query("SELECT areaname FROM {$DT_PRE}area where areaid in(".$r['filteraddress'].") ORDER  BY areaid");
			 while($r1 = $db->fetch_array($result1)) {
				$qy[] = $r1['areaname'];
			 }
			
			 $r['aaa']=implode(",",$qy);
			 
			}
			//行业分类
			$hyfl='';
			if($r['filtercategory']){
			 $result3 = $db->query("SELECT f_cat_name FROM {$DT_PRE}filtercategory where fid in(".$r['filtercategory'].") ORDER  BY displayorder");
			 while($r3 = $db->fetch_array($result3)) {
				$hyfl[] = $r3['f_cat_name'];
			 }
			  $r['bbb']=implode(",",$hyfl);
			}
			//招标分类
		    $zbfl='';		
			if($r['filtertender']){
			 $result2 = $db->query("SELECT catname FROM {$DT_PRE}category where catid in(".$r['filtertender'].") and moduleid=23 ORDER  BY catid");
			 while($r2 = $db->fetch_array($result2)) {
				$zbfl[] = $r2['catname'];
			 }
			  $r['ccc']=implode(",",$zbfl);
			}
			$r['adddate'] = timetodate($r['addtime'], 5);
			$r['editdate'] = timetodate($r['edittime'], 5);
			$r['gender'] = $SEX[$r['sex']];
			$r['status'] = $SUBSCRIBE[$r['subscribe']];
			$r['headimgurl'] or $r['headimgurl'] = 'api/weixin/image/headimg.jpg';
			$lists[] = $r;
		}
		
		include tpl('toemail', $module);
	break;
	

}


?>